<?php
namespace app\admin\controller;
use think\Controller;
use Caretree\Caretree;  //无线级分类  在extend文件夹下新建一个Caretree文件在创建一个Caretree.php 命名空间为 包名
class Category extends Controller 
{
    public function lst()
    {
		$Category = new Caretree();
		$CategoryObj= db('Category');
		if(request()->isPost()){
			$data = input('post.');
			
			// dump($data);
			// die;
			$Category->cateSort($data['sort'],$CategoryObj);
			$this->success('排序成功',url('lst'));
		}
		$CategoryRes = $CategoryObj->order('sort ASC')->select();
		$CategoryRes = $Category->Caretree($CategoryRes);
		$this->assign([
			'CategoryRes' => $CategoryRes
		]);
		return view('list');
	}
	public function add()
	{
		$Category = new Caretree();
		$CategoryObj= model('Category');
		if(request()->isPost()){
			$data = input('post.');
			
			//处理上传图片
			if($_FILES['cate_img']['tmp_name']){
				$data['cate_img']=$this->upload();
			}
			//验证
    		// $validate = validate('Category');
    		// if(!$validate->check($data)){
			//     $this->error($validate->getError());
			// }
			$add= $CategoryObj->save($data);
			
			if($add){
				$this->success('添加分类成功!','lst');
			}else{
				$this->error('添加分类失败');
			}
			return;
		}
		//商品分类推荐位
		$categoryRecposRes = db('recpos')->where('rec_type','=',2)->select();
		$CategoryRes = $CategoryObj->order('sort DESC')->select();
		$CategoryRes = $Category->Caretree($CategoryRes);
		//商品类型
		$typeRes =  db('type')->select();
		$this->assign([
			'CategoryRes'=>$CategoryRes,
			'categoryRecposRes'=>$categoryRecposRes,
			'typeRes'=>$typeRes
			]);
		return view();
	}
	public function edit()
	{
		$Category = new Caretree();
		$CategoryObj= model('Category');
		if(request()->isPost()){
			$data = input('post.');
			if($_FILES['cate_img']['tmp_name']){
				$data['cate_img']=$this->upload();
				$oldImg =$CategoryObj->field('cate_img')->find($data['id']); 
				if($oldImg['cate_img']){
					$imgSrc = IMG_UPLOADS.DS.$oldImg['cate_img'];
						if(file_exists($imgSrc)){
							@unlink($imgSrc);
						}
			}
		}
			//验证
    		// $validate = validate('Category');
    		// if(!$validate->check($data)){
			//     $this->error($validate->getError());
			// }
			$save= $CategoryObj->update($data);
			if($save !== false){
				$this->success('修改分类成功!','lst');
			}else{
				$this->error('修改分类失败');
			}
			return;
		}
		//商品分类推荐位
		$categoryRecposRes = db('recpos')->where('rec_type','=',2)->select();
		$_myCategoryRecposRes = db('rec_item')->where(array('value_type'=>2,'value_id'=>input('id')))->select();
		
		$myCategoryRecposRes = array();
		foreach($_myCategoryRecposRes as $k => $v){
			$myCategoryRecposRes[] = $v['recpos_id'];
		}
		// dump($categoryRecposRes);
		// dump($myCategoryRecposRes);
		// die;
		$Categorys = $CategoryObj->find(input('id'));
		$CategoryRes = $CategoryObj->order('sort DESC')->select();
		$CategoryRes = $Category ->Caretree($CategoryRes);
		//商品类型
		$typeRes =  db('type')->select();
		//商品属性
		$searchAttrArr = array();

		$attrIds = $Categorys['search_attr_ids'];
		if($attrIds){
			$searchAttrArr = db('attr')->field('id,attr_name,type_id')->where(array('id'=>array('in',$attrIds)))->select();
		}
		$this->assign([
			'CategoryRes' => $CategoryRes,
			'Categorys' => $Categorys,
			'categoryRecposRes'=>$categoryRecposRes,
			'myCategoryRecposRes'=>$myCategoryRecposRes,
			'typeRes'=>$typeRes,
			'searchAttrArr'=>$searchAttrArr
		]);
		return view();
	}
	public function del($id)
	{	$Category = db('Category');
		$Caretree = new Caretree();
		$sonids = $Caretree->childrenids($id,$Category);
		//dump($sonids);
		$sonids[] = intval($id); //新增一个整型值
		
		// //删除分类前判断该分类下的文章和文章和相关的缩略图 
		// $article = db('article');
		// foreach ($sonids as $k => $v){

		// 	//通过tp_Category的id字段  查询article表中的id和缩略图字段
		// 	$artRes = $article->field('id,thumb')->where(array('Category_id'=>$v))->select();
		// 	foreach ($artRes as $k1 => $v1){
		// 		//遍历字段thumb拼接图片的绝对路径
		// 		$thumbSrc = IMG_UPLOADS.$v1['thumb'];
		// 		if(file_exists($thumbSrc)){
		// 			@unlink($thumbSrc);
		// 		}
		// 		//通过id字段删除该文章
		// 		$article->delete($v1['id']);
		// 	}
		// }
		//删除栏目前检查并删除栏目的推荐信息
		$recItem = db('recItem');
		foreach ($sonids as $k => $v){
			$recItem->where(array('value_id'=>$v,'value_type'=>2))->delete();
		}
		$del=$Category->delete($sonids);
    	if($del){
			$this->success('删除分类成功！','lst');
		}else{
			$this->error('删除分类失败！');
		}
		return view();
	}
	public function upload(){
	    // 获取表单上传文件 例如上传了001.jpg
	    $file = request()->file('cate_img');
	    
	    // 移动到框架应用根目录/public/uploads/ 目录下
	    if($file){
	        $info = $file->move(ROOT_PATH . 'public' .DS.'static'. DS . 'uploads');
	        if($info){
	            return $info->getSaveName();
	        }else{
	            // 上传失败获取错误信息
	            echo $file->getError();
				die;
	        }
	    }
	}
	
}
